Working of Constructors in vb.net

Ratings:
(4)
Views: 0
Banner-Img
Share this blog:

 

Constructors:

It is used to initialize the members of the class VB.Net supports two types of constructors

  • Instance constructors
  • Shared constructors

A) Instance Constructors:

It is used to initialize the instance and the shared members of the class   

Syntax:

[Modifier] sub new ([arg info])                                                                                                 

statement(s)                                                                                                                             

- - - - - - -                                                                                                                                               

- - - - - -                                                                                                                                           

End sub

Note:

  • Instance constructors will be invoked every time a new object is created for the class.
  • A class can have any number of instance constructors.
  • If a class is defined with more than one constructor then it is said to be the classes overloaded with constructors.
  • If a constructor is defined using arguments then it is said to be a parameterized constructor
  • If a constructor is defined without any arguments then it is said to the zero-argument constructor.

Inclined to build a profession as VB.Net Developer? Then here is the blog post on, explore VB.Net Training

B) Shared Constructors:

It is used to initialize the shared members of the class.

Syntax:

Shared sub new ( )                                                                                                

statement(s)                                                                                                                                   

- - - - - -                                                                                                                                           

- - - - - -                                                                                                                                           

End sub        

Observation:

  • Shared constructors will be invoked only once whenever the class definition is loaded.
  • A class can have a maximum of only one shared constructor.
  • Shared constructors can’t accept any arguments.

Project [menu] à Add new item à Module

Name: Constructors ‘‘Demo

‘‘‘Demo on constructors . . . . .

Imports system

Module constructors ‘‘Demo

Class Test

Public i as integer

Public shared s as an integer

Sub new( ) ‘Instance constructor’

P = 100

S = 1000

End sub

Shared sub new( ) ‘Shared constructor’

S = 500

P = 500 error

End sub

End class

Sub main ( )

Console.WriteLine (“value of s before object creation: {0}”, Test.s)

Dim t As new Test

Console.WriteLine (“Value of t and s” after object creation: {0}, {1}”,                           t.p, Test.s)                        

End sub

End module

    Shared constructor  

Observation:

  • Whenever the value for the shared members has to be initialized before creating an object or if a constant value has to be assigned for the shared member then shared constructors should be used to initialize the shared members.
  • Whenever the value for the shared member has to be reinitialized with a new value for every new instance created for a class then the instance constructor should be used to initialize the shared members.

For an in-depth knowledge, click on below

You liked the article?

Like: 0

Vote for difficulty

Current difficulty (Avg): Medium

EasyMediumHardDifficultExpert
IMPROVE ARTICLEReport Issue

About Author

Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.